namespace Brjnk.GpxExplorer.UI.ValidationRules
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows.Data;
    using Brjnk.Common;

    internal class SearchPositionArgsEditorVr : ValidationRule
    {
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            var bg = value as BindingGroup;
            object newLimit, newSearchAgainLimit;

            if (!bg.TryGetValue(bg.Items[0], "Limit", out newLimit)) System.Diagnostics.Debug.WriteLine("Brjnk: Limit property not found");
            if (!bg.TryGetValue(bg.Items[0], "SearchAgainLimit", out newSearchAgainLimit)) System.Diagnostics.Debug.WriteLine("Brjnk: SearchAgainLimit not found");

            Distance newLimitD, newSearchAgainLimitD;
            try
            {
                newLimitD = Distance.Parser.Parse(newLimit as string);
                newSearchAgainLimitD = Distance.Parser.Parse(newSearchAgainLimit as string);
            }
            catch (Exception ex)
            {
                return new ValidationResult(false, null);
            }
            if (newLimitD > newSearchAgainLimitD) return new ValidationResult(false, null);

            return ValidationResult.ValidResult;
        }
    }
}
